package com.sydsen.imager;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @Auther: syh
 * @Date: 2021/10/29
 * @Description:
 */
public class Canvas {

    private int width;
    private int height;
    private ImageOptions.PicType picType;
    private BufferedImage image;
    private Graphics2D graphics2D;


    public Canvas(int width, int height) {
        this(width, height, ImageOptions.PicType.JPG);
    }

    public Canvas(int width, int height, ImageOptions.PicType picType) {
        this.width = width;
        this.height = height;
        this.picType = picType;
    }

    /**
     * 初始化画布
     * @return
     */
    public void initCanvas() {
        this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        graphics2D = image.createGraphics();

        if (ImageOptions.PicType.PNG.getName().equals(picType.getName())) {
            image = graphics2D.getDeviceConfiguration()
                    .createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            graphics2D = image.createGraphics();
        }

        //抗锯齿
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    public void setPicType(ImageOptions.PicType picType) {
        this.picType = picType;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public ImageOptions.PicType getPicType() {
        return picType;
    }

    public Graphics2D getGraphics() {
        return graphics2D;
    }

    public BufferedImage getImage() {
        return this.image;
    }

    public void dispose() {
        this.graphics2D.dispose();
    }
}
